define(['./PageView', "jquery", '../libs/AjaxUtil', '../libs/scrollBottom', '../libs/CommonTools'], function (PageView, $, AjaxUtil, ScrollBottom, Common) {
    require('../../less/OKRsDetail.less');

    return PageView.extend({

        events: {
            'click .log-head-left': 'goBack',
            'click .launch-btn': 'launch',
            'click .js-to-detail': 'toDetail',
            'click .add-log-button': 'addLog',
            'click .keyresult-add': 'addKeyResult',
            'click .target-edit': 'editTarget',
            'click .target-del': 'delTarget',
            'click .box-horizontal': 'goResultDetail',
        },
        default: true,
        initialize: function () {
            var _this = this;
            var selectView = _this.findComponent('OKRsSelectView');
            _this.selectView = selectView;
            _this.count = 10;
            this.scrollBottom = new ScrollBottom();

            this.params = {
                ownerType: 1,
                pageNo: 1
            };

            this.listenTo(_this.selectView, 'tabview-selectLeftItem-event', _this.updateList);
            this.listenTo(_this.selectView, 'tabview-selectRightItem-event', _this.updateList);
        },
        render: function () {
            PageView.prototype.render.apply(this, arguments);
            this.$wrap = $('<div class="main"><div class="content"></div></div><div class="okrs-bottom"><div class="keyresult-add">新增关键结果</div><div class="target-edit">编辑</div><div class="target-del">删除</div></div>');
            this.$el.append(this.$wrap);
            return this;
        },
        onResume: function () {
            this.getData();
        },
        onRender: function () {
            this.getData();
            return this;
        },
        getData: function () {
            var _this = this;
            var href = window.location.href;
            var start = href.indexOf("target_id");
            var id = href.substr(start+10,href.length);
            AjaxUtil.ajaxPost({
                path: '/client/objective/getById',
                params: {id:id},
                success: function (data) {
                    var $wrap = _this.$wrap.find('.content');
                    _this.data = data;
                    sessionStorage.setItem("goalData", JSON.stringify(data));
                    var temp = require('../templates/OKRsDetail.html');
                    temp = _.template(temp)(data);
                    $wrap.html(temp);

                    var totalProgress = 0;
                    data.keyResult.forEach(function (value, key) {
                        totalProgress += value.progress;
                    });
                    //占比共计在低于100时不显示
                    if(totalProgress>=100){
                        _this.$el.find("#total-progress").text("占比共计"+ totalProgress +"%");
                        _this.$el.find("#total-progress").show();
                    }
                    else{
                        _this.$el.find("#total-progress").hide();
                    }

                }
            });
        },
        addKeyResult: function () {
            var hrefStr = window.location.href;
            var start = hrefStr.indexOf("target_id");
            var target_id = hrefStr.substring(start+10,hrefStr.length);
            Backbone.history.navigate('#KeyResultAdd?target_id='+target_id, {trigger: true});
        },
        editTarget: function () {
            var hrefStr = window.location.href;
            var start = hrefStr.indexOf("target_id");
            var target_id = hrefStr.substring(start+10,hrefStr.length);
            Backbone.history.navigate('#GoalAddNEdit?target_id='+target_id,{
                trigger:true
            });
        },
        delGoal: function(){
            var _this = this;
            var hrefStr = window.location.href;
            var start = hrefStr.indexOf("target_id");
            var target_id = hrefStr.substring(start+10,hrefStr.length);
            AjaxUtil.ajaxPost({
                path: '/client/objective/delete',
                params: {id:target_id},
                success: function (data) {
                    Common.Toast("删除目标成功!",1000,function(){_this.goBack();});


                },
                error: function () {
                    Common.Toast("请检查网络!");
                }
            });
        },
        delTarget: function () {
            var _this = this;
            Common.confirm('删除目标将连同关键结果一起删除',function(){
                _this.delGoal();
            }, function(){});
        },
        goResultDetail: function(e){
            var keyId = $(e.currentTarget).attr("keyId");
            var hrefStr = window.location.href;
            var start = hrefStr.indexOf("target_id");
            var target_id = hrefStr.substring(start+10,hrefStr.length);
            Backbone.history.navigate('#KeyResultDetail?target_id='+target_id
                +'&key_id='+keyId,{
                trigger:true
            });
        },
        goBack: function(){
            Backbone.history.history.back();
        }
    });
});
